package com.kang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kang.bean.RoleMenus;
import com.kang.dto.RoleMenusDto;
import com.kang.mapper.RoleMenusMapper;
import com.kang.service.RoleMenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: service
 * @description:
 * @author: K.faWu
 * @create: 2022-03-03 16:34
 **/

@Service
public class RoleMenusServiceImpl extends ServiceImpl<RoleMenusMapper, RoleMenus> implements RoleMenusService {
    @Autowired
    private RoleMenusMapper roleMenusMapper;


    @Transactional
    @Override
    public void update(RoleMenusDto roleMenusDto) {
        QueryWrapper<RoleMenus> wrapper = new QueryWrapper<>();
        wrapper.eq("role_code", roleMenusDto.getRoleCode());
        roleMenusMapper.delete(wrapper);
        for (Long menusId: roleMenusDto.getMenusIdList()){
            RoleMenus roleMenus = new RoleMenus();
            roleMenus.setRoleCode(roleMenusDto.getRoleCode());
            roleMenus.setMenusId(menusId);
            roleMenusMapper.insert(roleMenus);
        }
    }
}
